// pages/community/results/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    tab: [
      "全部",
      "仅标题",
      "仅内容"
    ],
    searchKey: '',
    allList: [],
    list: [
      {
        "id": 7,
        "userID": "mxy",
        "plogTypeID": 3,
        "imagePath": "../../../../image/flower.png",
        "creatTime": "2022-06-06T01:07:12",
        "plogName": "运动啦",
        "plogContent": "今天你运动了没"
      }
    ]
  },
  /**
    * 切换
    */
  onChange(event) {
    console.log(event.detail)

    this.setData({
      active: event.detail.index
    })

    if (event.detail.index == 0) {
      this.setData({
        list: this.data.allList[0]
      })
    } else if (event.detail.index == 1) {
      this.setData({
        list: this.data.allList[1]
      })
    } else {
      this.setData({
        list: this.data.allList[2]
      })
    }
  },

  /**
   * 获取所有商品
   */
  getPlog() {
    var that = this

    wx.request({
      url: app.globalData.url + '/search_plog/',
      method: "GET",
      data: {
        searchContent: that.data.searchKey
      },
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)

        if (res.data.status_code == 500) {
          wx.showToast({
            title: '获取失败',
            duration: 2000,
            icon: 'error'
          })
        } else {
          that.setData({
            allList: res.data,
            list: res.data[0]
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '连接失败',
          duration: 2000,
          icon: 'error',
        })
      }
    })
  },

  /**
     * 跳转至详情
     */
  toDetailsTap: function (e) {
    console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: "../detail/index?id=" + e.currentTarget.dataset.id
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.searchKey)
    this.setData({
      searchKey: options.searchKey
    })
    this.getPlog()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})